/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deti.es.persistence.network;

import deti.es.persistence.account.AccountDoesNotExist;
import deti.es.ukni.persistence.entity.*;
import java.util.*;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 * Implementation of the {@link SocialNetworkRemote} interface.
 * 
 * @author ukni
 */
@Stateless( mappedName="ejb/MyBandSocialNetwork" )
public class SocialNetworkBean implements SocialNetworkRemote {

    @PersistenceContext
    private EntityManager manager;

    public SocialNetworkBean(){}

   /**
    * Implementation of (@link SocialNetworkRemote#addFriend)
    */
    public void addFriend(Account account, Integer IDFriend) throws AccountDoesNotExist {

        Account friend;
        Friend newFriend;
        try{
            friend = manager.find(Account.class, IDFriend);

            newFriend = new Friend( new FriendPK(account.getIdaccount(), friend.getIdaccount()) );
            manager.persist(newFriend);
        } catch( NoResultException e){
            // IDFriend não existe....
            throw new AccountDoesNotExist("User with ID " + IDFriend.toString() );
        }

    }

   /**
    * Implementation of (@link SocialNetworkRemote#followBand)
    */
    public void followBand(Account account, Integer IDBand) {

        Band band;

        band = manager.find(Band.class, IDBand);

        account.getBandCollection().add(band);
        manager.merge(account);
    }

   /**
    * Implementation of (@link SocialNetworkRemote#commentBand)
    */
    public void commentBand(Account account, String comment, Integer IDBand) {

        Band band;
        CommentBand commentBand;

        try{
            band = manager.find(Band.class, IDBand);

            Comment newComment = new Comment();

            newComment.setPostdate(new java.util.Date() );
            newComment.setContent(comment);

            manager.persist(newComment);
            commentBand = new CommentBand(new CommentBandPK(newComment.getIdcomment(), account.getIdaccount(), band.getIdband()));
            manager.persist(commentBand);
        } catch( NoResultException e){}

    }
   /**
    * Implementation of (@link SocialNetworkRemote#commentAudio)
    */
    public void commentAudio(Account account, String comment, Integer IDAudio) {

        Audio audio;
        CommentAudio commentAudio;

        try{
            audio = manager.find(Audio.class, IDAudio);

            Comment newComment = new Comment();
            newComment.setPostdate(new java.util.Date() );

            newComment.setContent(comment);
            manager.persist(newComment);
            commentAudio = new CommentAudio(new CommentAudioPK(newComment.getIdcomment(), account.getIdaccount(), audio.getIdaudio()));
            manager.persist(commentAudio);
        } catch( NoResultException e){}

    }

   /**
    * Implementation of (@link SocialNetworkRemote#commentEvent)
    */
    public void commentEvent(Account account, String comment, Integer IDEvent) {

        Event event;
        CommentEvent commentEvent;

        try{
            event = manager.find(Event.class, IDEvent);

            Comment newComment = new Comment();
            newComment.setContent(comment);
            newComment.setPostdate(new java.util.Date());
            manager.persist(newComment);

            commentEvent = new CommentEvent(new CommentEventPK(newComment.getIdcomment(), account.getIdaccount(), event.getIdevent()));
            manager.persist(commentEvent);
        } catch( NoResultException e){}

    }

   /**
    * Implementation of (@link SocialNetworkRemote#commentImage)
    */
    public void commentImage(Account account, String comment, Integer IDImage) {

        Image image;
        CommentImage commentImage;

        try{
            image = manager.find(Image.class, IDImage);

            Comment newComment = new Comment();
            newComment.setContent(comment);
            newComment.setPostdate(new java.util.Date());
            manager.persist(newComment);

            commentImage = new CommentImage(new CommentImagePK(newComment.getIdcomment(), account.getIdaccount(), image.getIdimage()));
            manager.persist(commentImage);
        } catch( NoResultException e){}

    }

   /**
    * Implementation of (@link SocialNetworkRemote#commentVideo)
    */
    public void commentVideo(Account account, String comment, Integer IDVideo) {

        Video video;
        CommentVideo commentVideo;

        try{
            video = manager.find(Video.class, IDVideo);

            Comment newComment = new Comment();
            newComment.setContent(comment);
            newComment.setPostdate(new java.util.Date());
            manager.persist(newComment);

            commentVideo = new CommentVideo(new CommentVideoPK(newComment.getIdcomment(), account.getIdaccount(), video.getIdvideo()));
            manager.persist(commentVideo);
        } catch( NoResultException e){}

    }

   /**
    * Implementation of (@link SocialNetworkRemote#viewFavoriteBands)
    */
    public Set<Band> viewFavoriteBands(Account account) {
        return account.getBandCollection();
    }

   /**
    * Implementation of (@link SocialNetworkRemote#viewFriends)
    */
    public Set<Friend> viewFriends(Account account) {
        return account.getFriendCollection();
    }

   /**
    * Implementation of (@link SocialNetworkRemote#viewFollowers)
    */
    public Set<Account> viewFollowers(Integer IDBand) {

        Band band;

        try{
            band = manager.find(Band.class, IDBand);

            return band.getAccountCollection();
        } catch( NoResultException e){}
        return null;
    }

}
